<?php
//header("Content-type: text/html; charset=utf-8");
 
include_once "site_config.php"; 
require_once "ra_global.php";
 
$id='shell';
$wxlist=array(
		"shell"		=>array(APPID,APPSECRET),
);


$appid = $wxlist[$id][0];
$appsecret = $wxlist[$id][1];

$mem = new Memcache;                                                                    
$mem->connect('localhost', 11211) or die ("Could not connect");
$access_token = $mem->get($id);

$access_token = '';

if (empty($access_token)){
	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;

	$res = http_request($url);
	$result = json_decode($res, true);
	$access_token = $result["access_token"];
	$mem->set($id, $access_token, 0, 7000);
} 
/*
	function http_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
*/

//创建菜单
function createMenu($data,$access_token){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$tmpInfo = curl_exec($ch);
	if (curl_errno($ch)) {
	  return curl_error($ch);
	}
	curl_close($ch);
	return $tmpInfo;
}
$data = '{
    "button": [
        {
            "name": "积分验证", 
            "sub_button": [
                {
                    "type": "scancode_waitmsg", 
                    "name": "扫码积分", 
                    "key": "scanoil"
                },
				{	
				   "type":"click",
				   "name":"输码积分", 
                   "key": "writeoil"
				},
				{	
				   "type":"view",
				   "name":"优惠码验证",
				   "url":"'.SiteUrl.'oildiscount.php"
				},
				{	
				   "type":"view",
				   "name":"积分红包",
				   "url":"'.SiteUrl.'hongbao.php"
				},
				{	
				   "type":"click",
				   "name":"规则说明",
				   "key":"readme"
				}
            ]
        },
		
		{
            "name": "用户反馈", 
            "sub_button": [
                {	
				   "type":"view",
				   "name":"意见箱",
				   "url":"'.SiteUrl.'opnion.php"
				},
				{	
				   "type":"view",
				   "name":"心愿单",
				   "url":"'.SiteUrl.'wishlist.php"
				}
            ]
        },
		{	
		   "type":"view",
		   "name":"个人中心",
		   "url":"'.SiteUrl.'user.php"
		}
    ]
}';
 

 
$data = '{
    "button": [
        {
            "name": "积分验证", 
            "sub_button": [
                {
                    "type": "scancode_waitmsg", 
                    "name": "扫码积分", 
                    "key": "scanoil"
                },
				{	
				   "type":"click",
				   "name":"输码积分", 
                   "key": "writeoil"
				},
				{	
				   "type":"view",
				   "name":"优惠码验证",
				   "url":"'.SiteUrl.'oildiscount.php"
				},
				{	
				   "type":"click",
				   "name":"积分红包",
				   "key":"jifenhongbao"
				},
				{	
				   "type":"click",
				   "name":"规则说明",
				   "key":"readme"
				}
            ]
        },
		
		{
            "name": "用户反馈", 
            "sub_button": [
                {	
				   "type":"view",
				   "name":"意见箱",
				   "url":"'.SiteUrl.'opnion.php"
				},
				{	
				   "type":"view",
				   "name":"心愿单",
				   "url":"'.SiteUrl.'wishlist.php"
				}
            ]
        },
		{	
		   "type":"view",
		   "name":"个人中心",
		   "url":"'.SiteUrl.'user.php"
		}
    ]
}';
 

echo createMenu($data,$access_token);//创建菜单
?>